using System;
using System.ComponentModel;
using System.Threading;
using Hangfire;
using Hangfire.Console;
using Hangfire.RecurringJobExtensions;
using Hangfire.Server;

namespace Yozeev.Service
{
    /// <summary>
    /// 周期性作业测试类
    /// </summary>
    public class RecurringJobService
    {
        [DisplayName("TestJob1")]
        [AutomaticRetry(Attempts = 5)] //最大重试次数值
        [RecurringJob("*/10 * * * *")]
        [Queue("default")]
        public void TestJob1(PerformContext context)
        {
            context.WriteLine($"{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")} TestJob1 Running ...");

            //在没有异常的情况下仅处理一次，若发生异常，提供重试机制
            var jobId = BackgroundJob.Enqueue(
                    () => Console.WriteLine("Fire-and-forget!"));

            ////延迟（计划）任务跟队列任务相似，客户端调用时需要指定在一定时间间隔后调用：
            var job2Id = BackgroundJob.Schedule(
                () => Console.WriteLine("Delayed!"),
                TimeSpan.FromDays(7));

            //定时（循环）任务代表可以重复性执行多次，支持CRON表达式：
            RecurringJob.AddOrUpdate("执行时间：" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"),
                    () => Console.WriteLine("Recurring!"),
                    Cron.Hourly, queue: "jobs");

            ////延续性任务类似于.NET中的Task,可以在第一个任务执行完之后紧接着再次执行另外的任务：
            BackgroundJob.ContinueWith(
                jobId,
                () => Console.WriteLine("Continuation!"));

        }
        [DisplayName("TestJobYoyo")]
        [RecurringJob("*/1 * * * *")]
        [Queue("default")]
        public void TestJobYoyo()
        {
            Console.WriteLine("TestJobYoyo test....");
        }

        [RecurringJob("*/2 * * * *", RecurringJobId = "TestJob2")]
        [Queue("default")]
        public void TestJob2(PerformContext context)
        {
            context.WriteLine($"{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")} TestJob2 Running ...");
        }
        [RecurringJob("*/2 * * * *", "China Standard Time", "jobs")]
        public void TestJob3(PerformContext context)
        {
            context.WriteLine($"{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")} TestJob3 Running ...");
        }
        [RecurringJob("*/5 * * * *", "default")]
        public void InstanceTestJob(PerformContext context)
        {
            context.WriteLine($"{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")} InstanceTestJob Running ...");
        }

        [RecurringJob("*/6 * * * *", "UTC", "jobs")]
        public static void StaticTestJob(PerformContext context)
        {
            context.WriteLine($"{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")} StaticTestJob Running ...");
        }

        [RecurringJob("*/6 * * * *")]
        public static void TestDefaultJob(PerformContext context)
        {
            context.WriteLine($"{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")} TestDefaultJob Running ...");
        }
        [RecurringJob("*/6 * * * *", "default")]
        public static void TestDefaultJob2(PerformContext context)
        {
            context.WriteLine($"{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")} TestDefaultJob2 Running ...");
        }
        [RecurringJob("*/6 * * * *")]
        public static void TestDefaultJob66(PerformContext context)
        {
         //   throw new Exception(@"System.Exception: 123
        //    at RLsir.school.HangFire.Service.RecurringJobService.TestDefaultJob66(PerformContext context) in ");
        }

        public static void TestEnqueueJob(PerformContext context)
        {
            //throw new Exception("TestEnqueueJob");
        }
        [Queue("jobs")]
        public static void TestEnqueueJob321(PerformContext context)
        {
            //throw new Exception("TestEnqueueJob.......");
        }
        [RecurringJob("*/9 * * * *")]
        [Queue("default")]
        public static void TestEnqueueJob123(PerformContext context)
        {
            Thread.Sleep(3000);
            throw new Exception("TestEnqueueJob");
        }
        [Queue("jobs")]
        public void MethodQueue()
        {
            Console.WriteLine("方法队列测试。。。");
        }
    }
}
